﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Radiation___IM.Commands
{
    public class Motd : ICommand
    {
        private readonly string m_commandkey = "motd";

        public Motd()
        {
        }

        public string commandKey()
        {
            return m_commandkey;
        }

        public string requiredPermission()
        {
            return "motd";
        }

        public void execute(string[] command_parts, Client client)
        {
            if (command_parts.Length > 1 && command_parts[0].Length > 0)
            {
                StringBuilder final_motd = new StringBuilder();
                for (int i = 1; i < command_parts.Length; i++)
                {
                    if (command_parts[i] != null)
                        final_motd.Append(command_parts[i]);

                    final_motd.Append(" ");
                }
                string motd = final_motd.ToString();

                if (motd.Length > 0)
                {
                    if (motd.EndsWith(" "))
                        motd.Substring(0, motd.Length - 1); // delete the last space

                    if (ChannelManager.Instance[command_parts[0]] != null)
                        ChannelManager.Instance[command_parts[0]].Motd = motd;
                    else
                        MessageExecution.sendData(client, "Wreid error : the channel you used to send this message does not exist");
                }
                else
                    MessageExecution.sendData(client, "Your Motd message is empty.");
            }
            else
                MessageExecution.sendData(client, "The username you provided is too short / does not exist.");
        }
    }
}
